#include "menu.h"

#include <iostream>

#include "order.h"
#include "bill.h"
#include "claim.h"
#include "tender.h"
#include "data_io.h"

///body file for class Menu

Menu::Menu(std::vector<Document*> *p_menu_v_ptr) throw() : menu_v_ptr(p_menu_v_ptr)
{
	Menu_options_enum m=INVALID_CHOICE;
	do
	{
		write_main_menu();
		std::cout<<"Valasszon: ";
		m=get_menu_choice();
		do_chosen_menu(m);
	}while(EXIT_AND_SAVE!=m);
}

Menu::~Menu() throw()
{
}

void Menu::write_main_menu() const throw()
{
	std::cout<<std::endl<<
	"Menu:"<<std::endl<<
	"Uj irat:"<<std::endl<<
	"   [ h ]atarozat"<<std::endl<<
	"   Ugyfeltol erkezo level:"<<std::endl<<
	"      [ a ]janlatkeres"<<std::endl<<
	"      [ p ]anaszos level"<<std::endl<<
	"   [ s ]zamla"<<std::endl<<
	"Lekerdezesek:"<<std::endl<<
	"   Pa[ r ]tnerre"<<std::endl<<
	"   [ u ]gyszamra"<<std::endl<<
	"   [ i ]ktatoszamra"<<std::endl<<
	"[ k ]ilepes es mentes"<<std::endl;
}

void Menu::add_document(const Document::Document_types &p_type) throw()
{
	std::cout<<"Irat hozzadasa: "<<Document::document_type_name(p_type)<<std::endl;
	Document* d;
	switch(p_type)
	{
		case Document::ORDER:
		{
			d=new Order;
			break;
		}
		case Document::BILL:
		{
			d=new Bill;
			break;
		}
		case Document::CLAIM:
		{
			d=new Claim;
			break;
		}
		case Document::TENDER:
		{
			d=new Tender;
			break;
		}
		default:
		{
			break;
		}
	}
	d->fill_in_document_data(std::cin);
	menu_v_ptr->push_back(d);
}

void Menu::query_partner() const throw()
{
	///Date_IO::get_string(std::cin) does not throw anything
	std::cout<<"Kereses partner alapjan."<<std::endl;
	std::cout<<"Partner    ? ";
	std::string p_partner;
	p_partner=Data_IO::get_string(std::cin);
	bool found=false;
	int count_bill=0;
	int total=0;
	bool overflow=false;
	for(unsigned int i=0;i<menu_v_ptr->size();++i)
	{
		if("*" == p_partner || std::string::npos!=menu_v_ptr->at(i)->get_partner().find(p_partner))
		{
			int actual_bill_total;
			actual_bill_total=(menu_v_ptr->at(i)->print_out_document_data(std::cout));
			total+=actual_bill_total;
			if(total<actual_bill_total)
			{
				overflow=(overflow || true);
			}
			if(1==menu_v_ptr->at(i)->get_type())
			{
				++count_bill;
			}
			if(!found)
			{
				found=true;
			}
		}
	}
	if(!found)
	{
		std::cout<<"Nincsen talalat."<<std::endl;
	}
	else
	{
		if(0!=count_bill)
		{
			std::cout<<std::endl<<"A talalati listaban szereplo "<<count_bill<<" darab szamla osszerteke: "<<total<<" Ft."<<std::endl;
			if(overflow)
			{
				std::cerr<<"Tulcsordulas!"<<std::endl;
			}
		}
		else
		{
			std::cout<<std::endl<<"A talalati listaban nem szerepel szamla."<<std::endl;
		}
	}
}

void Menu::query_issue_no() const throw()
{
	///Date_IO::get_natural(std::cin) does not throw anything
	std::cout<<"Kereses ugyszam alapjan."<<std::endl;
	std::cout<<"Ugyszam    ? ";
	int p_issue_no;
	p_issue_no=Data_IO::get_natural(std::cin);
	bool found=false;
	for(unsigned int i=0;i<menu_v_ptr->size();++i)
	{
		if(p_issue_no==menu_v_ptr->at(i)->get_issue_no())
		{
			menu_v_ptr->at(i)->print_out_document_data(std::cout);
			if(!found)
			{
				found=true;
			}
		}
	}
	if(!found)
	{
		std::cout<<"Nincsen talalat."<<std::endl;
	}
}

void Menu::query_ref_no() const throw()
{
	///Date_IO::get_natural(std::cin) does not throw anything
	std::cout<<"Kereses iktatoszam alapjan."<<std::endl;
	std::cout<<"Iktatoszam ? ";
	int p_ref_no;
	p_ref_no=Data_IO::get_natural(std::cin);
	bool found=false;
	for(unsigned int i=0;i<menu_v_ptr->size();++i)
	{
		if(p_ref_no==menu_v_ptr->at(i)->get_ref_no())
		{
			menu_v_ptr->at(i)->print_out_document_data(std::cout);
			if(!found)
			{
				found=true;
			}
		}
	}
	if(!found)
	{
		std::cout<<"Nincsen talalat."<<std::endl;
	}
}

Menu::Menu_options_enum Menu::get_menu_choice() const throw()
{
	bool p_ok=true;
  char p_ch;
	Menu_options_enum p_m=INVALID_CHOICE;
	do
	{
		std::string p_s;
		p_s=Data_IO::get_string(std::cin);
		p_ch=p_s[0];
		switch(p_ch)
		{
			case 'h':
			{
				///hatarozat
				p_m=ADD_ORDER;
				break;
			}
			case 'a':
			{
				///ajanlatkeres
				p_m=ADD_TENDER;
				break;
			}
			case 'p':
			{
				///panaszos level
				p_m=ADD_CLAIM;
				break;
			}
			case 's':
			{
				///szamla
				p_m=ADD_BILL;
				break;
			}
			case 'r':
			{
				///kereses partnerre
				p_m=QUERY_PARTNER;
				break;
			}
			case 'u':
			{
				///kereses ugyszamra
				p_m=QUERY_ISSUE;
				break;
			}
			case 'i':
			{
				///kereses iktatoszamra
				p_m=QUERY_REF_NO;
				break;
			}
			case 'k':
			{
				///kilepes es mentes
				p_m=EXIT_AND_SAVE;
				break;
			}
			default:
			{
				break;
			}
		}
		if(INVALID_CHOICE==p_m || p_s.length()!=1)
		{
			std::cerr<<"Ervenytelen valasztas."<<std::endl;
			std::cout<<"Valasszon: ";
			p_ok=false;
		}
		else
		{
			p_ok=true;
		}
	}while(!p_ok);
	return(p_m);
}

void Menu::do_chosen_menu(const Menu_options_enum &p_menu) throw()
{
	///adding document form menu does not throw anything
	switch(p_menu)
	{
		case ADD_ORDER:
		{
			add_document(Document::ORDER);
			break;
		}
		case ADD_TENDER:
		{
			add_document(Document::TENDER);
			break;
		}
		case ADD_CLAIM:
		{
			add_document(Document::CLAIM);
			break;
		}
		case ADD_BILL:
		{
			add_document(Document::BILL);
			break;
		}
		case QUERY_PARTNER:
		{
			query_partner();
			break;
		}
		case QUERY_ISSUE:
		{
			query_issue_no();
			break;
		}
		case QUERY_REF_NO:
		{
			query_ref_no();
			break;
		}
		case EXIT_AND_SAVE:
		{
			break;
		}
		default:
		{
			break;
		}
	}
}
